const Mock = require('mockjs')
const Random = Mock.Random
const phonePrefix = ['132', '135', '189']
const index = Math.floor(Math.random() * phonePrefix.length)
var phone = phonePrefix[index] + Mock.mock(/\d{8}/)

const data = Mock.mock({
  'items|100': [{
    id: '@increment',
    order: Random.id(),
    create_time: '@datetime',
    account: '@first @last',
    money: '@integer(60, 5000)',
    // 支付方式
    'payment|1': ['未支付', '支付宝', '微信'],
    'order_source|1': ['APP订单', 'WEB订单'],
    // 订单状态
    'order_status|1': ['已关闭', '待发货', '已发货', '已完成']
  }]
})
const orderData = Mock.mock({
  'status|1': ['1', '2', '3'],
  createTime: '@datetime',
  paymentTime: '@datetime',
  deliveryTime: '@datetime',
  receiveTime: '@datetime',
  commentTime: '@datetime',

  orderSn: Random.id(),
  memberUsername: '@first @last',
  payType: '未支付',
  'sourceType|1': ['APP订单', 'WEB订单'],
  orderType: '正常订单',
  'autoConfirmDay|1-100': 100,
  'integration|1-500': 1,
  'growth|0-10': 1,

  receiverName: '@first @last',
  receiverPhone: phone,
  receiverPostCode: '@email',

  'orderItemList|5': [{
    'productPic|1': ['http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/5ac1bf58Ndefaac16.jpg',
      'http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180615/5a9d248cN071f4959.jpg'],
    'productName|1': ['华为 HUAWEI P20', '小米', '荣耀'],
    'productBrand|1': ['华为', '小米'],
    'productPrice': '@integer(60, 5000)',
    'productSn|10000-99999': 10000,
    'productAttr': '[{"key":"颜色","value":"金色"},{"key":"容量","value":"16G"}]',
    'productQuantity|1-99': 2
  }]
})

module.exports = [
  {
    url: '/vue-admin-template/order/list',
    type: 'get',
    response: config => {
      // query 请求参数
      const { page = 1, limit = 20, order, create_time, account, payment, order_source, order_status } = config.query
      const mockList = data.items.filter(item => {
        let no = 1 // 1表示 该条数据 正确匹配

        if (order && item.order.indexOf(order) < 0) {
          // console.log("1>"+order)
          no = 0
        }
        if (create_time && item.create_time.indexOf(create_time) < 0) {
          // console.log("2>"+create_time)
          no = 0
        }
        if (account && item.account.indexOf(account) < 0) {
          // console.log("3>"+account)
          no = 0
        }
        if (payment && item.payment.indexOf(payment) < 0) {
          // console.log("4>"+payment)
          no = 0
        }
        if (order_source && item.order_source.indexOf(order_source) < 0) {
          // console.log("5>"+order_source)
          no = 0
        }
        if (order_status && item.order_status.indexOf(order_status) < 0) {
          // console.log("6>"+order_status)
          no = 0
        }
        if (no !== 1) { return false }
        return true
      })
      // 分页
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/vue-admin-template/order/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      console.log('id--order--:' + id)
      return {
        code: 20000,
        data: orderData
      }
    }
  }
]
